﻿namespace Windows32
{
    using System.Drawing;
    using System.Windows.Forms;
    using XDesignerCommon;

    public class RectangleMouseCapturer : MouseCapturer
    {
        protected bool bolCustomAction;
        protected int intDragStyle;
        protected Rectangle myDescRectangle;
        protected Rectangle mySourceRectangle;

        public RectangleMouseCapturer()
        {
            this.intDragStyle = 0;
            this.mySourceRectangle = Rectangle.Empty;
            this.myDescRectangle = Rectangle.Empty;
            this.bolCustomAction = false;
        }

        public RectangleMouseCapturer(Control ctl)
        {
            this.intDragStyle = 0;
            this.mySourceRectangle = Rectangle.Empty;
            this.myDescRectangle = Rectangle.Empty;
            this.bolCustomAction = false;
            base.myBindControl = ctl;
        }

        protected override void OnDraw()
        {
            using (ReversibleDrawer drawer = ReversibleDrawer.FromHwnd(this.BindControl.Handle))
            {
                drawer.PenStyle = PenStyle.PS_DOT;
                drawer.PenColor = Color.Red;
                int dx = this.CurrentPosition.X - this.StartPosition.X;
                int dy = this.CurrentPosition.Y - this.StartPosition.Y;
                Rectangle rect = this.UpdateRectangle(this.SourceRectangle, dx, dy);
                drawer.DrawRectangle(rect);
            }
        }

        protected override void OnMouseMove()
        {
            base.OnMouseMove();
            if (!this.bolCustomAction)
            {
                int dx = base.CurrentPosition.X - base.StartPosition.X;
                int dy = base.CurrentPosition.Y - base.StartPosition.Y;
                this.myDescRectangle = this.UpdateRectangle(this.mySourceRectangle, dx, dy);
            }
        }

        public Rectangle UpdateRectangle(Rectangle rect, int dx, int dy)
        {
            if (this.intDragStyle == -1)
            {
                rect.Offset(dx, dy);
            }
            if (((this.intDragStyle == 0) || (this.intDragStyle == 7)) || (this.intDragStyle == 6))
            {
                rect.Offset(dx, 0);
                rect.Width -= dx;
            }
            if (((this.intDragStyle == 0) || (this.intDragStyle == 1)) || (this.intDragStyle == 2))
            {
                rect.Offset(0, dy);
                rect.Height -= dy;
            }
            if (((this.intDragStyle == 2) || (this.intDragStyle == 3)) || (this.intDragStyle == 4))
            {
                rect.Width += dx;
            }
            if (((this.intDragStyle == 4) || (this.intDragStyle == 5)) || (this.intDragStyle == 6))
            {
                rect.Height += dy;
            }
            return rect;
        }

        public Rectangle CurrentRectangle
        {
            get
            {
                return RectangleCommon.GetRectangle(base.StartPosition, base.CurrentPosition);
            }
        }

        public bool CustomAction
        {
            get
            {
                return this.bolCustomAction;
            }
            set
            {
                this.bolCustomAction = value;
            }
        }

        public Rectangle DescRectangle
        {
            get
            {
                return this.myDescRectangle;
            }
            set
            {
                this.myDescRectangle = value;
            }
        }

        public int DragStyle
        {
            get
            {
                return this.intDragStyle;
            }
            set
            {
                this.intDragStyle = value;
            }
        }

        public Rectangle SourceRectangle
        {
            get
            {
                return this.mySourceRectangle;
            }
            set
            {
                this.mySourceRectangle = value;
            }
        }
    }
}

